<template>
  <div class="my">
    <div class="return">
      <router-link to="/"
        ><img src="./../assets/number/left.png" alt=""
      /></router-link>
    </div>
    <div class="topContent" v-if="logining">
      <div class="human">
        <div class="pic"><img src="./../assets/my/human.webp" alt="" /></div>
        <div class="humanCon">
          <div class="name">{{myname}}</div>
          <div class="number">账号:{{myAccount}}</div>
        </div>
        <div class="exit" v-if="logining == true" v-on:click="isexit = true">退出登录</div>
      </div>
      <div class="topOther">
        <div class="add">+添加标签</div>
        <div class="addCon">开朗</div>
        <div class="addCon">活泼</div>
        <div class="addCon">个性</div>
      </div>
    </div>
    <div class="noTop" v-else>
      <div class="human">
        <div class="humanCon">
          <div class="name" v-on:click="login">登录/注册</div>
          <div class="number">在这黑白的世界里寻找乐趣💥</div>
        </div>
      </div>
    </div>
    <div class="chooseTop">
      <li v-on:click= 'myIndex =0 ' :class="{active : myIndex == 0}">主页</li>
      <li v-on:click= 'myIndex =1 ' :class="{active : myIndex == 1}">荣誉</li>
      <li v-on:click= 'myIndex =2 ' :class="{active : myIndex == 2}">关于</li>
    </div>
    <ul class="chooseCon" v-if="logining">
      <li v-if="myIndex== 0">
        <div class="choose-title">我的主页</div>
        <div class="choose-content">
          暂时还未开发哦
        </div>
      </li>
      <li v-if="myIndex== 1">
        <div class="choose-title">我的荣誉</div>
        <div class="choose-content">
          暂时还未开发哦
        </div>
      </li>
      <li v-if="myIndex== 2">
        <div class="choose-title">关于</div>
        <div class="choose-content">
          暂时还未开发哦
        </div>
      </li>
    </ul>
    
    <div class="chooseno" v-else>
        <img src="./../assets/my/findnull.png" alt="">
        <div>还未登陆哦,快去登录吧</div>
    </div>
    <div class="showBox" v-if="isLoginBox">
      <div class="myBox">
        <div class="top">登录</div>
        <div class="input">
          <img src="./../assets/my/number.png" alt=""/>
          <input
            type="text"
            placeholder="请输入账号"
            v-model="account"
            @keydown.enter='loginBtn'
          />
        </div>
        <div class="input">
          <img class="psd" src="./../assets/my/psd.png" alt="" />
          <input type="text" placeholder="请输入密码" v-model="password" @keydown.enter='loginBtn'/>
        </div>
        <div class="bottom">
          <div class="loginBtn active" v-on:click="loginBtn">登录</div>
          <div class="registerBtn" v-on:click="toRegister">去注册</div>
        </div>
        <div class="cancel" v-on:click="cancel">
          <img src="./../assets/my/cancel.png" alt="" />
        </div>
      </div>
    </div>
    <div class="showBox" v-if="isRegisterBox">
      <div class="myBox">
        <div class="top">注册</div>
        <div class="input">
          <img src="./../assets/my/number.png" /><input
            type="text"
            placeholder="请输入用户名"
            v-model="username"
            @keydown.enter='registerBtn'
          />
        </div>
        <div class="input">
          <img src="./../assets/my/number.png" /><input
            type="text"
            placeholder="请输入账号"
            v-model="account"
          />
        </div>
        <div class="input">
          <img class="psd" src="./../assets/my/psd.png" /><input
            type="text"
            placeholder="请输入密码"
            v-model="password"
            @keydown.enter='registerBtn'
          />
        </div>
        <div class="bottom">
          <div class="loginBtn" v-on:click="toLogin">去登录</div>
          <div class="registerBtn active" v-on:click="registerBtn">注册</div>
        </div>
        <div class="cancel" v-on:click="cancel">
            <img src="./../assets/my/cancel.png" alt="" />
        </div>
      </div>
    </div>
    <div class="isOK isloginOK" v-if="isLoginOK">
      <div class="box">登录成功</div>
    </div>
    <div class="isOK isRegisterOK" v-if="isRegisterOK">
      <div class="box">注册成功</div>
    </div>
    <div class="showExit" v-if="isexit">
      <div class="box">
        <div class="top">是否退出登录</div>
        <div class="bottom">
          <div class="left" v-on:click="exit">确定</div>
          <div class="right" v-on:click="isexit = false">取消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
ul,li{
    margin: 0;
    padding: 0;
    list-style: none;
}
.my {
    background-color: #eeeeee;
  .return {
    position: absolute;
    left: 10px;
    top: 10px;
    img {
      width: 40px;
    }
  }
  .showExit {
      position: fixed;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.1);
      top: 0;
      .box {
        width: 280px;
        height: 120px;
        margin: 250px auto;
        padding-bottom: 40px;
        background: white;
        border-radius: 5px;
        padding-top: 16px;
        box-sizing: border-box;
        .top {
          letter-spacing: 10px;
        }
        .bottom {
          display: flex;
          justify-content: center;
          margin-top: 50px;
          font-size: 15px;
          > div {
            margin: 0 30px;
            cursor: pointer;
          }
        }
      }
    }
    .showBox {
        position: fixed;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.1);
        top: 0;
        .myBox {
          width: 370px;
          margin: 100px auto;
          // height: 250px;
          box-sizing: border-box;
          background: white;
          border-radius: 10px;
          position: relative;
          color: #3a3a3a;
          .cancel {
            position: absolute;
            top: 16px;
            right: 16px;
            cursor: pointer;
            img {
              width: 23px;
            }
          }
          .top {
            font-size: 20px;
            padding-top: 15px;
            box-sizing: border-box;
            letter-spacing: 10px;
          }
          .input {
            margin: 20px auto;
            width: 250px;
            position: relative;
            // display: flex;
            img {
              width: 43px;
              background-color: #4991f2;
              position: absolute;
              left: 0px;
              top: 1px;
            }
            input {
              width: 100%;
              height: 45px;
              display: block;
              box-sizing: border-box;
              outline: none;
              padding-left: 50px;
              border: 1px solid #ccc;
            }
            .psd {
              background-color: rgb(255, 176, 29);
              padding: 5px;
              box-sizing: border-box;
            }
          }
          .bottom {
            display: flex;
            box-sizing: border-box;
            margin-top: 40px;
            > div {
              width: 50%;
              padding: 10px;
              text-align: center;
              cursor: pointer;
              font-size: 18px;
              letter-spacing: 4px;
              color: #3a3a3a;
              border-radius: 0 0 10px 10px;
            }
            .active {
              background: #4991f2;
              color: white;
            }
          }
         }
    }
    .isOK {
      position: fixed;
      width: 100%;
      height: 100%;
      z-index: 101;
      top: 0;
      .box {
        margin: 280px auto;
      }
    }
    .isRegisterOK{
      .box {
        margin: 346px auto;
      }
    }
  .topContent,
  .noTop {
      background-color: #fff;
    padding: 15px 10px 60px;
    border-bottom: 1px solid #e2e3e4;
    .human {
      display: flex;
      background-color: #fff;
      margin: 40px 0px 5px;
      .pic {
        width: 80px;
        height: 80px;
        overflow: hidden;
        border-radius: 50%;
        margin: 0px 10px 0px 0px;
        img {
          width: 90px;
          margin: -10px auto 0;
          display: block;
        }
      }
      .humanCon {
        text-align: left;
        line-height: 50px;
        padding-left: 5px;
        box-sizing: border-box;
        .name {
          font-size: 26px;
          width: 170px;
          white-space:nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
      }
      .exit{
          flex: 1;
          line-height: 50px;
          text-align: right;
          font-size: 14px;
      }
    }
    .topOther {
      display: flex;
      flex-wrap: wrap;
      width: 90%;
      margin: auto;
      .add {
        border-radius: 20px;
        margin: 0px 10px 0px;
        color: white;
        padding: 3px 5px;
        background-color: #13b9c7;
      }
      .addCon {
        border: 1px solid #e2e3e4;
        border-radius: 20px;
        margin: 0px 3px;
        padding: 3px 5px;
      }
    }
  }
  .noTop{
    .humanCon{
      width: 100%;
      .name{
        width: 100% !important;
      }
      >div{
        text-align: center;
      }
    }
  }
  .chooseTop {
      background-color: #fff;
    display: flex;
    justify-content: space-around;
    margin-bottom: 10px;
    border-bottom: 1px solid #ccc;
    li {
      font-size: 18px;

      color: #999;
      letter-spacing: 3px;
      list-style-type: none;
      padding: 10px 5px;
    }
    .active {
        position: relative;
        color: black;
      &::after {
          content: '';
          width: 30px;
          position: absolute;
          left: 9px;
          display: block;
          top: 42px;
          height: 3px;
          background-color: #000;
      }
    }
  }
  .chooseCon {
    justify-content: space-around;      
      list-style-type: none;
    background-color: #fff;
    display: flex;
    li {
        padding-top: 15px;
        .choose-content{
          height: 300px;
          line-height: 300px;
        }
    }
  }
  .chooseno{
      background-color: #fff;
      padding: 30px 0;
      div{
          margin-top: 20px;
          color: #979797;
      }
  }
}
</style>

<script>
export default {
  data() {
    return {
      myIndex: 0,
      logining: false,
      index: 5,
      isLoginBox: false,
      isRegisterBox: false,
      account: "",
      password: "",
      userList: [],
      isRegisterOK: false,
      isLoginOK: false,
      username: "",
      myname: "",
      myAccount : "",
      isexit: false,
    };
  },
  created() {
    // localStorage.setItem('logining',false);
    this.userList = JSON.parse(localStorage.getItem("userList")) || [
      {
        account: "123456",
        password: "625404",
        username: "陈俊洲",
      },
    ];
    if (localStorage.logining == "true") {
      this.logining = true;
    } else {
      this.logining = false;
    }
    this.myname = localStorage.getItem("myname") || "";
    this.myAccount = localStorage.getItem("myAccount") || "";
  },
  methods: {
      login(){
        this.isLoginBox = true;
      },
    toLogin() {
      this.isLoginBox = true;
      this.isRegisterBox = false;
      this.account = "";
      this.password = "";
    },
    toRegister() {
      this.isLoginBox = false;
      this.isRegisterBox = true;
      this.account = "";
      this.password = "";
    },
    loginBtn() {
      if (this.account == "" || this.password == "") {
        alert("输入框不能为空!");
        return;
      }
      let isAccountSame = false;
      let isPassSame = false;
      let index = 0;
      for (let i = 0; i < this.userList.length; i++) {
        if (this.userList[i].account == this.account) {
          isAccountSame = true;
          if (this.userList[i].password == this.password) {
            isPassSame = true;
            index = i;
          }
          break;
        }
      }
      if (isAccountSame) {
        if (isPassSame) {
          this.isLoginOK = true;
          setTimeout(() => {
            this.account = "";
            this.password = "";
            this.isLoginBox = false;
            this.isLoginOK = false;
            this.logining = true;
          }, 1000);
          localStorage.setItem("logining", true);
          localStorage.setItem("myname", this.userList[index].username);
          localStorage.setItem("myAccount", this.userList[index].account);
          this.myname = this.userList[index].username;
          this.myAccount = this.userList[index].account;
        } else {
          alert("密码不正确");
          return;
        }
      } else {
        alert("账号不存在");
      }
    },
    registerBtn() {
      if (this.account == "" || this.password == "") {
        alert("输入框不能为空!");
        return;
      }
      let isAccountSame = false;
      let isUserSame = false;
      this.userList.forEach((element) => {
        if (element.username == this.username) {
          isUserSame = true;
        }
        if (element.account == this.account) {
          isAccountSame = true;
        }
      });
      if (isAccountSame) {
        alert("账号已存在");
        return;
      }
      if (isUserSame) {
        alert("用户名已存在");
        return;
      }
      let obj = {};
      obj.account = this.account;
      obj.password = this.password;
      obj.username = this.username;
      this.userList.push(obj);
      localStorage.setItem("userList", JSON.stringify(this.userList));
      this.isRegisterOK = true;

      setTimeout(() => {
        this.username = "";
        this.account = "";
        this.password = "";
        this.isRegisterOK = false;
        this.isRegisterBox = false;
      }, 1000);
    },
    keydown(){
      // console.log(11)
    },
    exit() {
      localStorage.setItem("logining", false);
      localStorage.removeItem("myname");
      localStorage.removeItem("myAccount");
      this.logining = false;
      this.isexit = false;
    },
    cancel() {
      this.isRegisterBox = false;
      this.isLoginBox = false;
    },
  },
};
</script>